package com.maila.calltaxi.view;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.ColorDrawable;
import android.media.MediaPlayer;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.maila.calltaxi.R;
import com.maila.calltaxi.data.Order;
import com.maila.calltaxi.utils.MediaManager;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 *抢单pop
 */

public class PushOrderPop extends PopupWindow {

    private View mMenuView;
    TextView tv_date, tv_position, tv_clintinfo, cancel_action, now_call;
    private ImageView btn_recode;
    List<AnimationDrawable> mAnimationDrawables;
    RelativeLayout rl_recode;

    public PushOrderPop(final Context context, final Order order, final CallCarListener callCarListener) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.view_push_order, null);
        AutoUtils.autoSize(mMenuView);
        tv_date = (TextView) mMenuView.findViewById(R.id.order_date);
        tv_position = (TextView) mMenuView.findViewById(R.id.order_position);
        tv_clintinfo = (TextView) mMenuView.findViewById(R.id.tv_clintinfo);
        cancel_action = (TextView) mMenuView.findViewById(R.id.cancel_action);
        now_call = (TextView) mMenuView.findViewById(R.id.now_call);
        btn_recode = (ImageView) mMenuView.findViewById(R.id.order_recode);
        rl_recode = (RelativeLayout) mMenuView.findViewById(R.id.rl_recode);
        tv_position.setText(order.getPositions());
        tv_date.setText(order.getDate());
        tv_clintinfo.setText(order.getClintname()+" "+order.getClintphone());
        mAnimationDrawables = new ArrayList<>();
        final AnimationDrawable animationDrawable = (AnimationDrawable) btn_recode.getBackground();
        rl_recode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //重置动画
                resetAnim(animationDrawable);
                animationDrawable.start();
                MediaManager.release();
                //开始实质播放
                MediaManager.playSound(order.getRecord(),
                        new MediaPlayer.OnCompletionListener() {
                            @Override
                            public void onCompletion(MediaPlayer mp) {
                                animationDrawable.selectDrawable(0);//显示动画第一帧
                                animationDrawable.stop();
                            }
                        });
            }
        });

        now_call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callCarListener.sendcall(order);
                dismiss();
            }
        });
        cancel_action.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
//            设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
//            设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.sharepopupanimation);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(80000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                return true;
            }
        });
    }

    private void resetAnim(AnimationDrawable animationDrawable) {
        if (!mAnimationDrawables.contains(animationDrawable)) {
            mAnimationDrawables.add(animationDrawable);
        }
        for (AnimationDrawable ad : mAnimationDrawables) {
            ad.selectDrawable(0);
            ad.stop();
        }

    }
    public  interface  CallCarListener{
   void      sendcall(Order order);
    }
}
